package com.example.mapper;

import com.example.domain.Resource;
import com.example.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select * from [dbo].[User] where UID LIKE '%${id}%'")
    List<User> selectUserById(Integer id);

    @Select("select UNAME from [dbo].[User] where UID = #{id} ")
    String selectNameById(Integer id);

    @Select("select UNAME from [dbo].[User]")
    List<String> selectUname();

    @Select("select UID from [dbo].[User] where UNAME = #{uname}")
    Integer selectIdByUname(String uname);

    @Select("select count(*) from [dbo].[User] where UID = #{id} ")
    Integer hasId(String id);

    @Select("select LIMIT from [dbo].[User] where UID = #{id}")
    User selectLimitById(Integer id);

    @Select("select * from [dbo].[User] where concat (ISNULL(UID,''),ISNULL(UNAME,'')) LIKE '%${object}%'")
    List<User> selectUser(String object)throws Exception;

//    concat (ISNULL(UID,''),ISNULL(UNAME,'')) LIKE '%${object}%'
    @Select("select * from [dbo].[User] where ISNULL(UNAME,'') LIKE '%${name}%' AND ISNULL (UID,null) LIKE '%${id}%'")
    List<User> searchUserByAll(Integer id,String name)throws Exception;

    @Select("select * from [dbo].[User] where UNAME LIKE '%${name}%'")
    List<User> selectUidByUname(String name);

    @Select("select * from [dbo].[User]")
    List<User> showAllUser();

    @Update("update [dbo].[User] set UNAME = #{name} where UID = #{id}")
    Boolean updateUser(String id,String name);

    @Insert("insert into [dbo].[User] (UID,UNAME,LIMIT) values (#{id},#{name},'普通权限')")
    Boolean insertUser(String id,String name);

    @Delete("delete from [dbo].[User] where LIMIT = '普通权限'")
    Boolean deleteUser();

    //=========================下面是针对管理员的操作======================================
    //添加管理员和移除管理员
    @Update("update [dbo].[User] set LIMIT = '管理员' where UID = #{id}")
    Boolean addManager(Integer id);

    @Update("update [dbo].[User] set LIMIT = '普通权限' where UID = #{id}")
    Boolean removeManger(Integer id);

    @Select("select * from [dbo].[User] where LIMIT = '管理员'")
    List<User> showManager();
}
